package lumis.portal.serviceinstance.serialization;

import org.w3c.dom.Document;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.progress.ProgressConfig.Type;
import lumis.portal.serialization.DeserializationContext;
import lumis.portal.serialization.SerializationException;
import lumis.portal.serialization.operation.ConfigPersistence;
import lumis.portal.serialization.operation.DependentConfigOperationCollection;
import lumis.portal.transaction.ITransaction;

/**
 * Context class for service instances deserialization 
 *
 * @version $Revision: 9947 $ $Date: 2008-12-04 13:35:12 -0200 (Thu, 04 Dec 2008) $
 * @since 4.0.8
 */
public class ServiceInstanceDeserializationContext extends ServiceInstanceBaseSerializationContext
{
	private final DeserializationContext deserializationContext;
	private boolean creating = true;

	public ServiceInstanceDeserializationContext(final DeserializationContext deserializationContext)
	{
		super(deserializationContext);
		this.deserializationContext = deserializationContext;
	}

	public DeserializationContext getDeserializationContext()
	{
		return deserializationContext;
	}

	@Override
	public SessionConfig getSessionConfig()
	{
		return deserializationContext.getSessionConfig();
	}

	@Override
	public ITransaction getTransaction()
	{
		return deserializationContext.getTransaction();
	}	
	
	public String getIdMapping(String originalId)
	{
		return deserializationContext.getIdMapping(originalId);
	}

	public String getMappedServiceInstanceId()
	{
		return getIdMapping(serviceInstanceId);
	}
	
	public void applyForeignKeyFieldIdMappings(Object target, String[] fieldNames) throws SerializationException
	{
		deserializationContext.applyForeignKeyFieldIdMappings(target, fieldNames);
	}

	public void applyPrimaryKeyFieldIdMappings(Object target, String[] fieldNames) throws SerializationException
	{
		deserializationContext.applyPrimaryKeyFieldIdMappings(target, fieldNames);
	}
	
	public boolean isCreateNewIds()
	{
		return deserializationContext.isCreateNewIds();
	}

	public boolean isImportAsChild()
	{
		return deserializationContext.isImportAsChild();
	}

	public boolean isUpdateIfElementExists()
	{
		return deserializationContext.isUpdateIfElementExists();
	}

	public void sendProgressNotification(String message, Type type) throws PortalException
	{
		deserializationContext.sendProgressNotification(message, type, null);
	}

	public void sendProgressNotification(Document messageDoc, String styleFilename, Type type) throws PortalException
	{
		deserializationContext.sendProgressNotification(messageDoc, styleFilename, type);
	}
	
	public void sendProgressFinalNotification(String message, Type type) throws PortalException
	{
		deserializationContext.sendProgressFinalNotification(message, type);
	}

	public boolean isMustRollBack()
	{
		return deserializationContext.isMustRollBack();
	}

	public void setMustRollBack()
	{
		deserializationContext.setMustRollBack();
	}
	
	public DependentConfigOperationCollection<ConfigPersistence> getDependentConfigPersistences()
	{
		return deserializationContext.getDependentConfigPersistences();
	}
	
	public void persistDependentConfigs(String dependencyKey) throws PortalException
	{
		deserializationContext.persistDependentConfigs(dependencyKey);
	}

	/**
	 * Returns whether the service instance is being created or updated.
	 * 
	 * @return true if being created.
	 * @since 4.2.0
	 */
	public boolean isCreating()
	{
		return creating;
	}

	public void setCreating(boolean creating)
	{
		this.creating = creating;
	}
}
